package com.studyyoun.android.nonghe.home.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.FullyLinearLayoutManager;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.model.HomeMsgMainModel;
import com.studyyoun.android.nonghe.home.model.UserCheckModel;
import com.studyyoun.android.nonghe.home.model.UserCheckStatuRecodeModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

import org.xml.sax.XMLReader;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by androidlongs on 2017/11/20.
 */

public class InformationDetailsActivity extends CommonBaseActivity {

    private TextView mUserCountTextView;
    private TextView mUserNameTextView;
    private LinearLayout mStatue2LinearLayout;
    private LinearLayout mStatue1LinearLayout;
    private Button mStatue1Button;
    private TextView mStatue1TextView;
    private Button mStatue2Button;
    private TextView mStatue2TextView;
    private RecyclerView mContentRecyclerView;
    private Button mMoreButton;
    private TextView mTitleTextView;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_information_layout;
    }

    @Override
    protected void commonInitView(View view) {

        mUserNameTextView = findViewById(R.id.tv_user_name);
        mUserCountTextView = findViewById(R.id.tv_user_count);

        mStatue1LinearLayout = findViewById(R.id.ll_check_statue1);
        mStatue2LinearLayout = findViewById(R.id.ll_check_statue2);

        mStatue1Button = findViewById(R.id.bt_check_statue1);
        mStatue1TextView = findViewById(R.id.tv_check_statue1);

        mStatue2Button = findViewById(R.id.bt_check_statue2);
        mStatue2TextView = findViewById(R.id.tv_check_statue2);

        mMoreButton = findViewById(R.id.bt_more_check_statue);

        mContentRecyclerView = findViewById(R.id.rv_check_statue);

        mTitleTextView = findViewById(R.id.tv_base_header_title);
    }

    @Override
    protected void commonFunction() {
        mTitleTextView.setText("我的检测认证");
        findViewById(R.id.ll_base_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InformationDetailsActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadingStatueHistory();
        final UserModel.DataBean lData = CommonUserHelper.getUserModel().getData();

        String url = HttpUrlHelper.sUserCheckStatueUrl;

        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("userId", lData.getUserId() + "");


        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack(UserCheckModel.class) {
            @Override
            public void onSuccess(Object obj) {
                if (obj != null) {
                    final UserCheckModel lUserCheckModel = (UserCheckModel) obj;
                    if (lUserCheckModel != null) {

                        int lFlag = lUserCheckModel.getFlag();
                        if (lFlag == 0) {
                            mStatue1LinearLayout.setVisibility(View.GONE);
                            mStatue2LinearLayout.setVisibility(View.VISIBLE);

                            mStatue2Button.setText(lUserCheckModel.getButInfo());
                            mStatue2TextView.setText(lUserCheckModel.getInfo());
                            mStatue2TextView.setText(Html.fromHtml(lUserCheckModel.getInfo()));
                            mStatue2TextView.setClickable(true);
                            mStatue2TextView.setMovementMethod(LinkMovementMethod.getInstance());

                            mStatue2Button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String info = lUserCheckModel.getInfo();
                                    if (!TextUtils.isEmpty(info)) {
                                        info = info.replaceAll("<br/>", "\n");
                                    }
                                    ToastUtils.show("" + info, mContext);
                                }
                            });
                        } else if (lFlag == 1) {

//                            {
//                                "ButInfo": "立即检测",
//                                    "Info": "本地检测进行中,请立即检测,<br/>截止日期至2017年10月28日停止",
//                                    "Flag": 1
//                            }

                            mStatue1LinearLayout.setVisibility(View.VISIBLE);
                            mStatue2LinearLayout.setVisibility(View.GONE);

                            mStatue1Button.setText(lUserCheckModel.getButInfo());

                            mStatue1TextView.setText(lUserCheckModel.getInfo());
                            mStatue1TextView.setText(Html.fromHtml(lUserCheckModel.getInfo()));
                            mStatue1TextView.setClickable(true);
                            mStatue1TextView.setMovementMethod(LinkMovementMethod.getInstance());

                            mStatue1Button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
//                                    Intent lIntent = new Intent(mContext, ScanFaceActivity.class);
                                    Intent lIntent = new Intent(mContext, ScanFaceTestActivity.class);
                                    lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    mContext.startActivity(lIntent);
                                }
                            });

                        } else {
                            ToastUtils.show("暂无状态信息 ", mContext);
                        }

                    } else {
                        ToastUtils.show("暂无状态信息 ", mContext);
                    }
                } else {
                    ToastUtils.show("暂无状态信息 ", mContext);
                }

                loadingStatueHistory();
            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("暂无状态信息 ", mContext);
                loadingStatueHistory();
            }
        });

    }

    @Override
    protected void commonDelayFunction() {

        UserModel lUserModel = CommonUserHelper.getUserModel();
        if (lUserModel != null && lUserModel.getData() != null) {
            String lUserName = lUserModel.getData().getUserName();
            if (TextUtils.isEmpty(lUserName)) {
                lUserName = "***";
            }
            mUserNameTextView.setText("姓名 ：" + lUserName);

            String lSfzCode = lUserModel.getData().getSfzCode();
            if (TextUtils.isEmpty(lSfzCode)) {
                lSfzCode = "***";
            }
            mUserCountTextView.setText("身份证号 ：" + lSfzCode);
        } else {
            ToastUtils.show("请登录", mContext);
            return;
        }


        mMoreButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lIntent = new Intent(mContext, MoreCheckStatueActivity.class);
                lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lIntent);
            }
        });


    }

    private List<UserCheckStatuRecodeModel.DataBean> mDataBeanList;

    private void loadingStatueHistory() {
        String url = HttpUrlHelper.sUserGetCheckListUrl;

        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("pageIndex", "1");
        key.put("pageSize", "5");
        key.put("userId", "" + CommonUserHelper.getUserModel().getData().getUserId());

        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack(UserCheckStatuRecodeModel.class) {
            @Override
            public void onSuccess(Object obj) {
                if (obj != null) {
                    UserCheckStatuRecodeModel lUserCheckStatuRecodeModel = (UserCheckStatuRecodeModel) obj;
                    List<UserCheckStatuRecodeModel.DataBean> lData = lUserCheckStatuRecodeModel.getData();
                    if (lData != null) {
                        mDataBeanList = lData;
                        setMsgDataFunction();
                    } else {
                        ToastUtils.show("暂无检测记录", mContext);
                    }
                } else {
                    ToastUtils.show("暂无检测记录", mContext);
                }

            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("暂无最新公告 ", mContext);
            }
        });
    }


    private HomeMsgDataAdapter mHomeMsgDataAdapter;

    private void setMsgDataFunction() {
        if (mHomeMsgDataAdapter == null) {
            mHomeMsgDataAdapter = new HomeMsgDataAdapter(mContext, mDataBeanList);
            mContentRecyclerView.setLayoutManager(new FullyLinearLayoutManager(mContext));
            mContentRecyclerView.setAdapter(mHomeMsgDataAdapter);
        } else {
            mHomeMsgDataAdapter.setListDatas(mDataBeanList);
            mHomeMsgDataAdapter.notifyDataSetChanged();
        }


    }


    public class HomeMsgDataAdapter extends RecyclerView.Adapter<HomeMsgDataHolder> {

        private Context mContext;


        public HomeMsgDataAdapter(Context pContext, List<UserCheckStatuRecodeModel.DataBean> pDataBeanList) {
            mDataBeanList = pDataBeanList;
            mContext = pContext;
        }

        @Override
        public HomeMsgDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View lView = View.inflate(mContext, R.layout.home_item_layout_3, null);
            return new HomeMsgDataHolder(lView, mContext);
        }

        @Override
        public void onBindViewHolder(HomeMsgDataHolder holder, int position) {

            UserCheckStatuRecodeModel.DataBean lDataBean = mDataBeanList.get(position);
            holder.setDatas(lDataBean, position);
        }

        @Override
        public int getItemCount() {
            return mDataBeanList == null ? 0 : mDataBeanList.size();
        }

        private List<UserCheckStatuRecodeModel.DataBean> mDataBeanList;

        public void setListDatas(List<UserCheckStatuRecodeModel.DataBean> pListDatas) {
            mDataBeanList = pListDatas;
        }
    }


    public class HomeMsgDataHolder extends RecyclerView.ViewHolder {


        private final TextView mItemTimeTextView;
        private final TextView mCheckStatueTextView;
        private final View mItemHeaderView;
        private final Drawable mDrawable2;
        private final Drawable mDrawable1;
        private final TextView mMonyTextView;
        private final TextView mMonyTimeTextView;


        public HomeMsgDataHolder(View itemView, Context pContext) {
            super(itemView);

            mItemTimeTextView = itemView.findViewById(R.id.tv_item_time);
            mCheckStatueTextView = itemView.findViewById(R.id.tv_item_check_statue);
            mItemHeaderView = itemView.findViewById(R.id.v_item_check_statue);

            mMonyTextView = itemView.findViewById(R.id.tv_item_monay_statue);

            mMonyTimeTextView = itemView.findViewById(R.id.tv_item_monay_time);

            mDrawable1 = pContext.getResources().getDrawable(R.drawable.find_item_icon_1);
            mDrawable2 = pContext.getResources().getDrawable(R.drawable.find_item_icon_2);
        }

        public void setDatas(final UserCheckStatuRecodeModel.DataBean pDataBean, int pPosition) {


            String lAddTime = pDataBean.getAddTime();
            if (TextUtils.isEmpty(lAddTime)) {
                lAddTime = "***";
            }
            mItemTimeTextView.setText(lAddTime);


            String checkStatue = "检测已通过";
            int lFlag = pDataBean.getFlag();
            if (lFlag == 2) {
                checkStatue = "检测未通过";
                mItemHeaderView.setBackground(mDrawable1);
                mCheckStatueTextView.setTextColor(Color.parseColor("#FFFA3939"));
            } else {
                checkStatue = "检测已通过";
                mItemHeaderView.setBackground(mDrawable2);
                mCheckStatueTextView.setTextColor(Color.parseColor("#51f553"));
            }

            mCheckStatueTextView.setText(checkStatue);

            String monyString = "已发放";

            int lIsRelease = pDataBean.getIsRelease();
            if (lIsRelease == 1) {
                monyString = "已发放";
            } else {
                monyString = "未发放";
            }

            mMonyTextView.setText("资金发放：" + monyString);

            mMonyTimeTextView.setVisibility(View.GONE);
            mMonyTimeTextView.setText("发放日期：" + pDataBean.getReleaseTime());
        }
    }

}
